package com.cgt.xiaotao.user.controller;

import com.cgt.common.utils.R;
import com.cgt.xiaotao.user.domain.vo.SignResultVO;
import com.cgt.xiaotao.user.service.ISignRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Api(tags = "签到相关接口")
@RestController
@RequestMapping("sign-records")
@RequiredArgsConstructor
public class SignRecordController {

    private final ISignRecordService recordService;

    @ApiOperation("签到功能接口")
    @PostMapping
    public R<SignResultVO> addSignRecords() {
        try {
            SignResultVO signResultVO = recordService.addSignRecords();
            return R.ok("签到成功", signResultVO);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("补签接口")
    @PostMapping("/repair")
    public R<Integer> repairSign(@RequestParam String date) {
        try {
            Integer points = recordService.repairSign(date);
            return R.ok("补签成功", points);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("查询签到记录")
    @GetMapping
    public R<Byte[]> querySignRecords(
            @RequestParam(required = false) Integer year,
            @RequestParam(required = false) Integer month
    ) {
        try {
            Byte[] arr;
            if (year != null && month != null) {
                arr = recordService.querySignRecords(year, month);
            } else {
                arr = recordService.querySignRecords();
            }
            return R.ok(arr);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }


    @ApiOperation("查询我的总积分")
    @GetMapping("total")
    public R<Integer> queryTotalPoints() {
        try {
            Integer totalPoints = recordService.queryTotalPoints();
            return R.ok(totalPoints);
        }catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

}